package com.lantosec.hmrp.modules.category.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.category.entity.AssetsInfo;
import com.lantosec.hmrp.modules.category.entity.Statistical;
import com.lantosec.hmrp.modules.category.service.IAssetsInfoService;
import com.lantosec.hmrp.modules.category.service.IAssetsSupplierDirectoryService;
import com.lantosec.hmrp.modules.category.vo.AssetsInfoVO;
import com.lantosec.hmrp.modules.category.vo.StatisticalVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *
 * 资产信息表 控制器
 *
 * @author AutoCode
 * @since 2020-03-31
 */
@Api(value = "资产信息表控制器", tags = "资产信息表控制器")
@RestController
@RequestMapping("/category/info")
@Log4j2
public class AssetsInfoController extends BaseController {

    @Autowired
    private IAssetsInfoService service;

    @Autowired
    private IAssetsSupplierDirectoryService supplierDirectoryService;

    //@RequiresPermissions("user:show")
    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<AssetsInfoVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("checkPage")
    @ApiOperation(value = "查询已审核列表")
    public Result<Page<AssetsInfoVO>> page(
            @ApiParam(value = "分页对象", required = true)PageVO<AssetsInfo> page,
            AssetsInfoVO condition,
            @ApiParam(name ="权限分类，1个人2科室3全院" ,value = "permissionsType")
            @RequestParam(name = "permissionsType",required = false) Integer permissionsType){
        Page<AssetsInfo> infoPage = service.queryPage(page,condition,permissionsType);
        return Result.ok(entity2VO(infoPage));
    }

    @GetMapping("unCheckPage")
    @ApiOperation(value = "查询未审核列表")
    public Result<Page<AssetsInfoVO>> unCheckPage(
            @ApiParam(value = "分页对象", required = true)PageVO<AssetsInfo> page, AssetsInfoVO condition){
        Page<AssetsInfo> infoPage = service.unCheckPage(page,condition);
        return Result.ok(entity2VO(infoPage));
    }

    @GetMapping("get/{assetsId}")
    @ApiOperation(value = "根据assetsId查询资产信息")
    public Result<AssetsInfoVO> get(@ApiParam(value = "assetsId", required = true) @PathVariable(name="assetsId") String assetsId){
        AssetsInfo param = service.getInfoByAssetsId(assetsId);
        return Result.ok(entity2VO(param));
    }

    @GetMapping("getAssetsInfoByCode/{assetsCode}")
    @ApiOperation(value = "根据assetsCode查询资产信息")
    public Result<AssetsInfoVO> getAssetsInfoByCode(@ApiParam(value = "assetsCode", required = true) @PathVariable(name="assetsCode") String assetsCode){
        AssetsInfo param = service.getAssetsInfoByCode(assetsCode);
        return Result.ok(entity2VO(param));
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(
            @ApiParam(value = "资产信息表VO对象", required = true) @RequestBody @Validated AssetsInfoVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        AssetsInfo entity = vo.convertToEntity();
        service.create(entity);
        return Result.ok(entity.getAssetsId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "资产信息表VO对象", required = true) @RequestBody AssetsInfoVO vo){
        AssetsInfo entity = vo.convertToEntity();
        service.updateInfo(entity);
        return Result.ok();
    }

    @PutMapping("audit")
    @ApiOperation(value = "审核通过")
    public Result<Void> audit(
            @ApiParam(value = "固定资产ID",required = true) @RequestBody Map<String,Object> map)
    {
        service.audit((List) map.get("assetsIds"));
        return Result.ok();
    }

    @DeleteMapping(value = "changeState/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> changeState(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @PostMapping(value = "delete")
    @ApiOperation("物理删除")
    public Result<Void> delete(
            @ApiParam(value = "固定资产ID",required = true) @RequestBody Map<String,Object> map)
    {
        if(map.size() > 0){
            service.delete((List) map.get("assetsId"));
        }
        return Result.ok();
    }

    @PostMapping("createSupplier")
    @ApiOperation(value = "新增供应商或生产厂家")
    public Result<String> create(
            @ApiParam(value = "供应商或厂家名称",required = true) @RequestBody Map<String,String> map){
        return Result.ok(supplierDirectoryService.create(map.get("name")));
    }

    @GetMapping("statisticalByUserDept")
    @ApiOperation("固定资产按使用部门分类统计")
    public Result<List<StatisticalVO>> statisticalByUserDept(){
        List<Statistical> param = service.statisticalByUserDept();
        return Result.ok(entity2VO(param));
    }

    @GetMapping("statisticalBycategoryId")
    @ApiOperation("固定资产按资产分类统计")
    public Result<List<StatisticalVO>> statisticalBycategoryId(){
        List<Statistical> param = service.statisticalBycategoryId();
        return Result.ok(entity2VO(param));
    }

    @GetMapping("statisticalByLableType")
    @ApiOperation("固定资产按标签材质分类统计")
    public Result<List<StatisticalVO>> statisticalByLableType(){
        List<Statistical> param = service.statisticalByLableType();
        return Result.ok(entity2VO(param));
    }

    @GetMapping("statisticalByState")
    @ApiOperation("固定资产按状态分类统计")
    public Result<List<StatisticalVO>> statisticalByState(){
        List<Statistical> param = service.statisticalByState();
        return Result.ok(entity2VO(param));
    }

    @GetMapping("statisticalByPrice")
    @ApiOperation("固定资产按资产原值净值分类统计")
    public Result<List<StatisticalVO>> statisticalByPrice(){
        List<Statistical> param = service.statisticalByPrice();
        return Result.ok(entity2VO(param));
    }
}